package com.imageim.yimirror.common.ui.btn;

import android.app.Activity;
import android.content.Context;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.imageim.yimirror.R;
import com.imageim.yimirror.base.BaseActivity;
import com.imageim.yimirror.common.util.UserUtil;
import com.nineoldandroids.animation.ObjectAnimator;

import java.text.MessageFormat;

import top.wefor.circularanim.CircularAnim;

/**
 * Created by Bruce.Zhou on 2018/7/2 10:22.
 * Email: 907160968@qq.com
 */

public class BtnCircleClose extends RelativeLayout {

    private CountDownTimer timer;
    private int count = 300;
    private int totalTime = 0;

    public interface OnTimeOverListener {
        void onTimeOver();
    }

    private OnTimeOverListener onTimeOverListener;

    public void setOnTimeOverListener(OnTimeOverListener listener) {
        onTimeOverListener = listener;
    }

    public BtnCircleClose(Context context) {
        super(context);
        init();
    }

    public BtnCircleClose(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BtnCircleClose(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private TextView tvTime;
    private ImageView ivClose;
    private TextView tvClose;

    public interface OnCloseListener {
        void onClose();
    }

    private OnCloseListener mOnCloseListener;

    /**
     * 点击关闭按钮的操作
     */
    public void setOnCloseListener(OnCloseListener listener) {
        this.mOnCloseListener = listener;
    }

    private void init() {
        View view = View.inflate(getContext(), R.layout.layout_circle_btn_close, this);
        ivClose = view.findViewById(R.id.iv_close_btn);
        tvTime = view.findViewById(R.id.tv_time);
        tvClose = view.findViewById(R.id.tv_close);
        //        rotateCloseBtn(ivClose);

        ivClose.setOnClickListener(v -> CircularAnim.fullActivity((Activity) getContext(), view)
                .colorOrImageRes(R.color.black)
                .go(() -> {
                    if (mOnCloseListener != null) {
                        mOnCloseListener.onClose();
                    }

                    BaseActivity activity = (BaseActivity) getContext();
                    activity.finish();
                }));
    }

    private void rotateCloseBtn(ImageView view) {
        ObjectAnimator rotate = ObjectAnimator.ofFloat(view, "rotation", 0, 360 * 1000);
        rotate.setDuration(1000 * 800);
        rotate.setTarget(view);
        rotate.start();
    }

    /**
     * 设置关闭的图片和时间(秒)
     */
    public void setCloseImage(int resId, int time) {
        if (resId != 0) {
            ivClose.setImageResource(resId);
        }
        if (time == 0) {
            totalTime = 300;
            start(300);
        } else {
            totalTime = time;
            start(time);
        }
    }

    /**
     * 设置倒计时关闭文字和按钮颜色 文字颜色不可为null
     *
     * @param textColor 文字颜色
     * @param btnColor  按钮颜色  如果为0时，设置按钮颜色与文字颜色一致
     */
    public void setTextAndBtnColor(int textColor, int btnColor) {
        tvTime.setTextColor(textColor);
        if (btnColor == 0) {
            tvClose.setTextColor(textColor);
        } else {
            tvClose.setTextColor(btnColor);
        }
    }


    /**
     * 启动倒计时
     *
     * @param seconds 秒
     */
    public void start(int seconds) {
        if (timer == null) {
            count = seconds;
            rotateCloseBtn(ivClose);
            timer = new CountDownTimer(1000 * seconds, 1000) {
                @Override
                public void onTick(long l) {
                    tvTime.setText(MessageFormat.format("{0}s", l / 1000));
                    if (count > 0) {
                        count--;
                    }
                }

                @Override
                public void onFinish() {
                    count = totalTime;
                    if (onTimeOverListener != null) {
                        onTimeOverListener.onTimeOver();
                    }
                }
            }.start();
        }
    }

    public void restart(int seconds) {
        if (timer != null) {
            count = seconds;
            timer.cancel();
            timer = null;
            timer = new CountDownTimer(1000 * seconds, 1000) {
                @Override
                public void onTick(long l) {
                    tvTime.setText(MessageFormat.format("{0}s", l / 1000));

                    if (count > 0) {
                        count--;
                    }
                }

                @Override
                public void onFinish() {
                    count = totalTime;
                    if (onTimeOverListener != null) {
                        onTimeOverListener.onTimeOver();
                    }
                }
            }.start();
        }
    }

    public void destory() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        ivClose.clearAnimation();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        destory();
    }

    public int getCount() {
        return count;
    }
}
